package org.jiamandan.com.utils;

import org.apache.commons.lang.StringUtils;
import org.jiamandan.com.dto.DXBasicDto;


public class DXObjectUtils {
    private static final int[] number = {4, 6, 2, 16, 4, 4};


    public static DXBasicDto parse(String payload) {
        DXBasicDto dto = new DXBasicDto();
        int cursor = 0, length = payload.length();
        int start = 0, end =  number[cursor];
        dto.setStartPrefix(StringUtils.substring(payload, start, end));
        start = end;
        end += number[++cursor];
        dto.setReserved(StringUtils.substring(payload, start, end));
        start = end;
        end += number[++cursor];
        dto.setProtocol(StringUtils.substring(payload, start, end));
        start = end;
        end += number[++cursor];
        dto.setDeviceNo(StringUtils.substring(payload, start, end));
        start = end;
        end += number[++cursor];
        dto.setCmdLength(StringUtils.substring(payload, start, end));
        start = end;
        end += number[++cursor];
        dto.setCmd(StringUtils.substring(payload, start,end).toUpperCase());
        start = end;
        dto.setData(StringUtils.substring(payload, start, length - 4));
        dto.setValidate(StringUtils.substring(payload, length - 4, length));
        return dto;
    }

    /**
     * 嘉满电-DX应答拼接报文
     *
     * @param payload
     * @return
     */
    public static String replyMessage(String payload, DXBasicDto dto,String gunNo,  String cmd) {
        //“序列号域+加密标志(固定：00)+帧类型标志(cmd)+消息体”
        StringBuilder content = new StringBuilder()
                .append(StringUtils.leftPad(cmd, 4, "0"))
                .append(payload);
        StringBuilder builder = new StringBuilder();
        //为帧头 2 字节+保留 4 字节+地址 ADDR 两字节+长度两字节+SLEN+CRC 校验两字节
        //4 + 8 + 4 + 4 + 4 + SLEN
        builder.append("3AA3")
                .append(dto.getReserved())
                .append(dto.getProtocol())
                .append(dto.getDeviceNo())
                .append(StringUtils.leftPad(Integer.toHexString(content.length() / 2), 4, "0"))
                .append(cmd)
                .append(payload)
                .append(CommonUtil.getCRC16(builder.toString()));
        return builder.toString();
    }

    public static void main(String[] args) {
        String s = "3AA300000000000102140F030000B794005B81310000200002000000FECF20319DA4260A268EC9355B5F3C341F0741A221E0834EE434FA3B2F513FB264D882B73B2D4A64C069172C82941FFC9B53C5C4A7FE03C03AA97C656AFF58F1F2CAE2F49348F8339B8A84DDE6BF40E1C26B566EC3D506ADD912C7D6A5FC01C26CABBB1A3990576FC2D4AA1561AAC6D7A5EB18B1B289E3F5C659438B2322C5DDE52685B2BDA15EF7F4CC1C0A25B606CD65743876BB12D5EE4056F887E0284445667EFE3D895510135AFB45542AF6E03AA9CEDD0C545E9922999AA55FA70949A7C11B65FC3454E230A1CC7E7D9C34B249B5BAA6BD862FE8D07D6B85BADE1579781A5421888BB1DACC4F007BB21A7BFCE4DC1F614A00A96E56FBED232C5893FFEE9CD2E0494A711B0DAEA1BB73DBDA3D251DDEE48B2188E63CACCAD84189E96FE5DC5CE6DEE525298BFFE93718BD14D3E8465025C1A56D0100633A7EBDC9D5B8A1F93B9D745F46EC189B8A8455A6CB80C3363B8594FC5592AA07117FD3A46F0312203A5BF2F1CAA0B6B51900C86061C69DE5F4388F45538018BE176952FCEA1181E52D7A7B1841BC7F1B17F24F99250336BAAC9D1DBB12D5EE4056F884E0284445267FFE3D3D5538214FBB20FE4F0AE13203AA97AF0214276F18D30617643DC003FD6BEAC4C47CEBE6815ADF4594ED5A89F8004429C6361F09F6865DF4F7CCA6B02B1FC30BA3A2055C65A6F8B31A03A25D3E97AA923F295A5225EE3B2A5900FFA69D3C";
        String crc16 = CommonUtil.getCRC16(s);
        System.out.println(crc16);
    }

    public static String Object2String(DXBasicDto dto) {
        StringBuilder content = new StringBuilder();
        content.append(dto.getStartPrefix())
                .append(dto.getReserved())
                .append(dto.getProtocol())
                .append(dto.getDeviceNo())
                .append(dto.getCmdLength())
                .append(dto.getCmd())
                .append(dto.getData())
                .append(dto.getValidate());
        return content.toString();
    }

    /**
     *
     * @param profile
     * @return
     */
    public static String getStopReason(String profile) {
        switch (profile) {
           case ("01"):
              return "59";  //达到预设时间
            case ("02"):    //充电电量到
                return "59";
            case ("03"):
                return "58";
            case ("04"):
                return "75";
            case ("05"):
                return "79";
            case ("06"):
                return "89";
           case ("07"):
                return "95";
            case ("08"):
                return "88";
            default:
                return profile;
        }
    }
}
